package com.zjlanyun.eplus.view.report.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zjlanyun.eplus.R;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import static com.zjlanyun.eplus.view.report.UtilConstants.DEFAULT_COUNT;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_RA_MODELID;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_RA_NAME;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_RA_TDOMAIN;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_RA_VIEWMODE;


/**
 * 作者：韩笑 on 2017/5/19 0019 09:29
 * <p>
 * 作用：菜单recyclerview适配器，加入依赖 compile 'com.android.support:recyclerview-v7:24.2.1'
 */


public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> implements View.OnClickListener,View.OnLongClickListener{
    private static final String TAG = "MenuAdapter";
    //数据
    private Context context;
    List<Map<String,String>> mList;
    //监听
    ItemClickListener listener;
    //控制点击变色
    Vector<Boolean> vector = new Vector<>();


    /**
     * 构造方法
     * @param mList 数据
     */
    public MenuAdapter(List<Map<String,String>> mList){
        this.mList = mList;
        //循环，使每条数据都为false（未选中）
        for(Map string :mList){
            vector.add(false);
        }
        //其中默认设置第一个被选中
        vector.set(DEFAULT_COUNT,true);
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null){
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.menuitem,parent,false);
        final MenuAdapter.ViewHolder hold= new MenuAdapter.ViewHolder(view);
        view.setOnClickListener(this);
        return hold;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //显示菜单条目名
        holder.menuName.setText(mList.get(position).get(KEY_RA_NAME));
        holder.itemView.setTag(position);
        if(vector.get(position)){
            //选中
            holder.menuName.setTextColor(Color.BLACK);
            holder.root.setBackgroundColor(Color.WHITE);
        }else{
            //未选中
            holder.menuName.setTextColor(Color.WHITE);
            holder.root.setBackgroundColor(0xFF63b9ef);
        }

    }

    @Override
    public void onClick(View v) {
        int position = (int)v.getTag();
        for (int i = 0; i <vector.size() ; i++){
            vector.set(i,false);
        }
        vector.set(position,true);
        notifyDataSetChanged();
        if(listener != null){
            listener.onItemClick(v, position);
            listener.itemClick(position,mList.get(position).get(KEY_RA_MODELID),mList.get(position).get(KEY_RA_VIEWMODE),mList.get(position).get(KEY_RA_TDOMAIN),mList.get(position).get(KEY_RA_NAME));
        }

    }

    @Override
    public boolean onLongClick(View v) {
        return true;
    }

    public interface ItemClickListener{
        void onItemClick(View v, int position);
        void itemClick(int position, String menuName, String type, String sql ,String title);
    }
    public void setOnClickListener(ItemClickListener listener){
        this.listener = listener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView menuName;
        LinearLayout root;
        public ViewHolder(View view){
            super(view);
            menuName = (TextView) view.findViewById(R.id.menuName);
            root = (LinearLayout) view.findViewById(R.id.root);
        }
    }


}
